package com.mengjian.myspringai.helper;

import com.mengjian.myspringai.model.FileUploadModel;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xieren
 * created by 2025/7/3
 */
@Component
public class OssUploadHelper {

    @Resource
    private RestTemplate restTemplate;

    public FileUploadModel ossUploadPrivate(byte[] stream, String name, String prefix) {
        try {
            FileUploadModel upload = this.uploadFile(stream,name,
                    "hll315-com-private-static", getPrefix(prefix), 3600L);
            return upload;
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex.getMessage());
            return null;
        }
    }

    public String getPrefix(String prefix) {
        if (StringUtils.isEmpty(prefix)) {
            return prefix;
        } else {
            LocalDateTime now = LocalDateTime.now();
            String format = now.format(DateTimeFormatter.ISO_LOCAL_DATE);
            return prefix + "/" + format;
        }
    }

    private FileUploadModel uploadFile(byte[] fileByte, String fileName, String bucket, String fileKey, Long expiredInSec) throws IOException {

        var splits = fileName.split("\\.");


        var prefix = "test/" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        // 1、封装请求头
        var headers = new HttpHeaders();
        var contentType = MediaType.parseMediaType("multipart/form-data");

        headers.setContentType(contentType);
        headers.setContentDispositionFormData("media", fileName);
        // 2、封装请求体
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        var resource = new ByteArrayResource(fileByte) {
            @Override
            public String getFilename() {
                return fileName;
            }
        };

        param.add("file", resource);
        param.add("bucket", bucket);
        param.add("prefix", prefix);
        param.add("fileKey", "");
        param.add("expiredInSec", expiredInSec);

        // 3、封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
        // 4、发送请求
        String fileServiceUri = "http://172.16.44.7:6103";

        ResponseEntity<FileUploadModel> data = restTemplate.postForEntity(
                String.format("%s/v1/files/upload", fileServiceUri),
                formEntity,
                FileUploadModel.class);
        return data.getBody();
    }


}
